package android.slc.user.ui.user.vmbox

import android.slc.adapter.LoadMoreDataFormat3
import android.slc.adapter.PageModel
import android.slc.adapter.SelectBox
import android.slc.adapter.SelectUtils
import android.slc.appbase.api.slc.callback.SlcObserver
import android.slc.appbase.repository.remote.PageTransformUtils
import android.slc.appbase.repository.remote.SelectTransformUtils
import android.slc.appbase.ui.vd.wrapper.BaseListPageWrapper
import android.slc.appbase.ui.vd.wrapper.IBaseListPageWrapper
import android.slc.commonlibrary.util.compat.SlcCollectionUtils
import android.slc.extras.component.tree.ui.TreeListMultipleVmBox
import android.slc.extras.user.entity.UserInfo
import android.slc.extras.user.repository.remote.UserServiceRepository
import android.slc.user.R
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.ToastUtils

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 2:42
 *
 */
class UserListMultipleVmBox : TreeListMultipleVmBox<UserInfo, SelectBox<UserInfo>>(),
    IBaseListPageWrapper<SelectBox<UserInfo>> {

    private val baseListPageWrapper: BaseListPageWrapper<SelectBox<UserInfo>>

    init {
        baseListPageWrapper = object : BaseListPageWrapper<SelectBox<UserInfo>>(this) {

            override fun onLoadMore() {
                this@UserListMultipleVmBox.onLoadMore()
            }
        }
    }

    override fun getDataListSlcObserver(): SlcObserver<PageModel<SelectBox<UserInfo>>> {
        return baseListPageWrapper.dataListSlcObserver
    }

    override fun getLoadMoreDataFormat3(): LoadMoreDataFormat3 {
        return baseListPageWrapper.loadMoreDataFormat3
    }

    override fun refresh() {
        baseListPageWrapper.refresh()
    }

    override fun onSucceed(data: PageModel<SelectBox<UserInfo>>) {
        this.loadMoreDataFormat3.formatSimple(dataList, data)
        dataListOf.notifyChange()
    }

    override fun onError(errorCode: Int, errorMessage: String?) {
        onFailed(errorCode, errorMessage)
        this.loadMoreDataFormat3.loadMoreFail()
    }

    final override fun loadMore() {
        super.loadMore()
    }

    override fun onLoadMore() {
        loadMoreTree()
    }

    private fun getTreeId(): Long {
        return try {
            mTreeId.toLong()
        } catch (e: NumberFormatException) {
            0
        }
    }

    override fun loadMoreTree() {
        when {
            getTreeId() != 0L -> {
                UserServiceRepository.page(getTreeId(), loadMoreDataFormat3.offset)
                    .map(SelectTransformUtils.transitionSelectBoxPageMap())
                    .map {
                        if (it.isSuccess) {
                            SelectUtils.fillSelect(
                                getSelectUserInfoList(),
                                it.data.records
                            )
                        }
                        return@map it
                    }
                    .subscribe(dataListSlcObserver)
            }
            CollectionUtils.isNotEmpty(mLeafIdList) -> {
                UserServiceRepository.listByIds(
                    SlcCollectionUtils.transformT2(mLeafIdList,
                        CollectionUtils.Transformer { return@Transformer it.toString() })
                )
                    .map(SelectTransformUtils.transitionSelectBoxPageMap())
                    .map {
                        if (it.isSuccess) {
                            SelectUtils.fillSelect(
                                getSelectUserInfoList(),
                                it.data.records
                            )
                        }
                        return@map it
                    }
                    .subscribe(dataListSlcObserver)
            }
            else -> {
                dataListSlcObserver.onNext(PageTransformUtils.getPageModelResult())
                ToastUtils.showShort(R.string.user_label_user_info_not_found)
            }
        }
    }


    /**
     * 获取已经选择的用户信息
     */
    fun getSelectUserInfoList(defDataList: List<UserInfo>? = null): MutableList<UserInfo> {
        if (CollectionUtils.isEmpty(this.dataList)) {
            return mutableListOf<UserInfo>().apply {
                defDataList?.let { ifDefDataList ->
                    addAll(ifDefDataList)
                }
            }
        }
        val selectUser = (CollectionUtils.select(this.dataList, CollectionUtils.Predicate {
            if (it is SelectBox<*>) {
                return@Predicate it.isChecked
            }
            return@Predicate false
        }) as List<*>).let {
            val dataList = mutableListOf<UserInfo>()
            CollectionUtils.forAllDo(it, CollectionUtils.Closure { _, item ->
                if (item is SelectBox<*>) {
                    if (item.data is UserInfo) {
                        dataList.add(item.data as UserInfo)
                    }
                }
            })
            return@let dataList
        }
        return selectUser
    }
}